/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	hey 
 * 
 * <p>描   述  :   UnSafeBridgeServiceImpl.java for com.pengtu.gsj.service.impl.check.zhgl.dovelop    </p>
 * 
 * <p>最后修改 : $: Sep 25, 2017-4:16:57 PM v 1.0.0	 hey   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.zhgl.dovelop;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.zhgl.DangerBridgeDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.zhgl.DangerBridge;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.zhgl.dovelop.UnSafeBridgeService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * UnSafeBridgeServiceImpl
 * 
 * Sep 25, 2017 4:16:57 PM
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class UnSafeBridgeServiceImpl implements UnSafeBridgeService{
	@Autowired
	private DangerBridgeDao dangerBridgeDao;
	
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	
	@Override
	public List<DangerBridge> getLargeRepairList(QueryFilter queryFilter){
		return dangerBridgeDao.find(queryFilter, "testYear", true);
	}
	
	@Override
	public Map<String, Object> getLargeRepairMap(String startTime, String endTime, String type, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<DangerBridge> largeRepairList = new ArrayList<DangerBridge>();
		List<DangerBridge> lastList = new ArrayList<DangerBridge>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		
		//现有危桥都改造或者全部都列入处置计划得分2
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		//现有危桥未改造或者未列入处置计划，每发现一座扣分0.1
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		//年度新增危桥改造率为100%得分0.5
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		//年度新增危桥未改造或者未列入处置计划，每发现一座扣分0.1
		double reserveMark = Double.parseDouble((String)jsonObject.get("reserveMark"));
		
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		
		//现场检查路线的名称
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("testYear", startTime, MatchType.GE);
		queryFilter.addFilter("testYear", String.valueOf(Integer.valueOf(endTime)-1), MatchType.LE);
		
		double total = 0; 		//检查得分
		double percentage = 0; 			//系统完善度
		double tableFull  = yearFull + poitMark*systemManageService.getCheckYear(endTime, startTime, 1); 	//满分
		List<String> checkList = null;
		int years[] = new int[systemManageService.getCheckYear(endTime, startTime, 1)];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		//获取年度新增危桥处置情况
		largeRepairList = this.getLargeRepairList(queryFilter);
		
		//获取倒数第二年的
		queryFilter = new QueryFilter();
		if ("0".equals(type)) {
			queryFilter.addFilter("testYear", String.valueOf(Integer.valueOf(endTime)-1), MatchType.EQ);
			checkList = Arrays.asList("0", "0", "0", "0", "0");
		}else {
			queryFilter.addFilter("testYear", startTime, MatchType.EQ);
			checkList = Arrays.asList("0", "0");
		}
		lastList = this.getLargeRepairList(queryFilter);
		double COMPARE_TEMP = 1e-8;
		double oraTemp = yearFull;
		for(DangerBridge dangerBridge : lastList) {
			if (100 - dangerBridge.getReOriginalPate() > COMPARE_TEMP) {
				oraTemp -= MathsUtils.formatDouble((dangerBridge.getOutOriginalBridge())*yearPart*1.0, 1);
			} 
		}
		oraTemp = oraTemp < 0 ? 0 : MathsUtils.formatDouble(oraTemp,1);
		total += oraTemp;
		checkList.set(0, String.valueOf(oraTemp));
		
		for (int i = 0; i < largeRepairList.size() ; i++) {
			double addTemp = poitMark;
			if (100 - largeRepairList.get(i).getReAddBridgePate() > COMPARE_TEMP) {
				addTemp -= MathsUtils.formatDouble((largeRepairList.get(i).getOutAddBridge())*reserveMark*1.0, 1);
			}
			addTemp = addTemp < 0 ? 0 : MathsUtils.formatDouble(addTemp,1);
			total += addTemp;
			checkList.set(i+1, String.valueOf(addTemp));
		}
		
		map.put("sum",  MathsUtils.formatDouble(total,2));
		//判断完善度
		percentage = MathsUtils.divDouble(total*100 , tableFull, 2) ;
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("percent", percentage);
		map.put("years", years);
		map.put("lastList", lastList);
		map.put("largeRepairList", largeRepairList);
		map.put("checkList", checkList);
		map.put("att", list);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
